package com.duleme.servlet;

import com.duleme.common.EncryptUtil.MD5EncryptUtils;
import com.duleme.common.EncodeUtils;
import com.duleme.dao.UserDao;
import com.duleme.dao.UserInfoDao;
import com.duleme.entity.Captcha;
import com.duleme.entity.User;
import com.duleme.entity.UserInfo;
import com.duleme.service.UserService;
import com.duleme.service.impl.UserServiceImpl;
import com.google.gson.Gson;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@WebServlet(name = "UserRegister2Servlet",urlPatterns = "/User/Register2")
public class UserRegister2Servlet extends HttpServlet {

    private Boolean initUserInfo(String account,String password,User u,HttpServletRequest request){
        if (account == null || password == null){
            return false;
        }else {
            UserService userService = new UserServiceImpl();
            UserInfo userInfo = new UserInfo();
            User user = userService.login(account, MD5EncryptUtils.md5Encrypt(password));
            if (user==null){
                System.out.println("init userInfo failed: user not found");
                return false;
            }else {
                if (userService.initUserInfo(user.getUserId())){
                    userInfo.setUserId(user.getUserId());
                    u.setUserId(user.getUserId());
                    request.getSession().setAttribute("userInfo",userInfo);
                    System.out.println("userInfo user id:"+userInfo.getUserId());
                    return true;
                }else {
                    System.out.println("init userInfo failed:insert failed");
                    return false;
                }
            }

        }
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        EncodeUtils.setEncoding(request,response);
        System.out.println("/User/Register2");
        PrintWriter writer=response.getWriter();
        Captcha captcha = (Captcha)request.getSession().getAttribute("phoneCaptcha");
        if (captcha == null){
            writer.print("2");
            System.out.println("没输入验证码");
        }else {
            String userAccount = captcha.getUserAccount();
            String password=request.getParameter("userPassword");
            String userName = request.getParameter("userName");
            String identify = request.getParameter("identify");
            UserService userService=new UserServiceImpl();
            System.out.print(userAccount+":"+password+" :"+userName+":"+identify);
            User user = new User();
            user.setUserAccount(userAccount);
            user.setUserPwd(MD5EncryptUtils.md5Encrypt(password));
            user.setIdentity(Integer.valueOf(identify));
            user.setUserName(userName);
            user.setCreateDate(new Date());
            Boolean register = userService.register(user);
            System.out.println(register);
            if(register && initUserInfo(userAccount,password,user,request)){
                Map<String,String> map = new HashMap<>();
                map.put("result","1");
                map.put("userIdentify",String.valueOf(user.getIdentity()));
                String json = new Gson().toJson(map);
                writer.print(json);
                System.out.println("注册成功");
                user.setAvatar("http://localhost:8080/avatar/user/default.png");
                request.getSession().setAttribute("user",user);
                request.getSession().setAttribute("login","1");
                User u = new UserDao().findUserByAccAndPwd(userAccount,MD5EncryptUtils.md5Encrypt(password));
                UserInfo userInfo = new UserInfoDao().findByUserId(u.getUserId());
                request.getSession().setAttribute("userInfo",userInfo);
                if(request.getSession().getAttribute("user") == null){
                    System.out.println("tan si tong");
                }
            }else{
                writer.print("0");
                System.out.println("注册失败");
            }
            writer.close();
        }

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }
}
